package com.example.java_chatroom.controller;

import com.example.java_chatroom.constant.Constants;
import com.example.java_chatroom.model.Friend;
import com.example.java_chatroom.model.MessageSession;
import com.example.java_chatroom.model.MessageSessionUserItem;
import com.example.java_chatroom.model.UserInfo;
import com.example.java_chatroom.service.MessageService;
import com.example.java_chatroom.service.MessageSessionService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MessageSessionController {
    @Autowired
    private MessageSessionService messageSessionService;

    @Autowired
    private MessageService messageService;

    @GetMapping("/sessionList")
    @ResponseBody
    public Object getSessionList(HttpServletRequest request){
        List<MessageSession> messageSessionList=new ArrayList<>();
        //1.获取当前用户id
        HttpSession session = request.getSession(false);
        if(session==null){
            log.info("[getSessionList] 获取session失败");
            return messageSessionList;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION);
        if(userInfo==null){
            log.info("[getSessionList] 获取用户失败");
        }
        //2.根据用户id去查询该用户有哪些会话
        List<Integer> sessionIds=messageSessionService.getSessionIdByUserId(userInfo.getUserId());
        //3.遍历会话id
        for(Integer sessionId: sessionIds){
            MessageSession messageSession=new MessageSession();
            messageSession.setSessionId(sessionId);
            //3.根据会话id查询该会话涉及的好友
            List<Friend> friends=messageSessionService.getFriendsBySessionId(sessionId,userInfo.getUserId());
            messageSession.setFriends(friends);
            //4.查询每个会话的最后一条消息
            String lastMessage=messageService.getLastMessageBySessionId(sessionId);
            if(lastMessage==null){
                messageSession.setLastMessage("");
            }else {
                messageSession.setLastMessage(lastMessage);
            }
            //messageSession.setLastMessage(lastMessage);
            messageSessionList.add(messageSession);
        }
        return messageSessionList;
    }

    @RequestMapping("/session")
    @ResponseBody
    @Transactional
    public Object addMessageSession(Integer toUserId, @SessionAttribute(Constants.USER_SESSION) UserInfo userInfo){
        Map<String,Integer> resp=new HashMap<>();
        if(userInfo==null){
            log.info("获取userInfo失败，userInfo:{}",userInfo);
        }
        //1.先对message_session表插入数据
        MessageSession messageSession=new MessageSession();
        Integer result=messageSessionService.addMessageSession(messageSession);
        //2.接着对message_session_user表插入数据
        //插入登录用户
        MessageSessionUserItem item1=new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setUserId(userInfo.getUserId());
        messageSessionService.addMessageSessionUser(item1);
        //3.接着插入好友
        MessageSessionUserItem item2=new MessageSessionUserItem();
        item2.setSessionId(messageSession.getSessionId());
        item2.setUserId(toUserId);
        System.out.println(item2.getUserId());
        messageSessionService.addMessageSessionUser(item2);
        resp.put("sessionId",messageSession.getSessionId());
        return resp;
    }
}
